//
//  ADHeaderView.m
//  JinChengYun
//
//  Created by 金橙云 on 2017/4/11.
//  Copyright © 2017年 金橙云. All rights reserved.
//

#define O_WIDTH [UIScreen mainScreen].bounds.size.width
#import "ADHeaderView.h"

#import "TodoHttpManager.h"
#import "MJExtension.h"
#import "ADHeaderModel.h"

@implementation ADHeaderView

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        
        
        //TODO:广告分析图
        _manager = [TodoHttpManager sharedManager];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didTDManagerSecondADTu:) name:TDManagerSecondADTu object:nil];
        [_manager userManagerSecondADTu];
        
        _adTDic = [NSMutableDictionary dictionary];
        _adLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, O_WIDTH, 40)];
        _adLab.backgroundColor = [UIColor whiteColor];
        _adLab.text = @"   广告费用 : 90000.00元";
        _adLab.textColor = [UIColor blackColor];
        _adLab.font = [UIFont systemFontOfSize:12];
        [self addSubview:_adLab];
        UILabel *lineLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 40, [UIScreen mainScreen].bounds.size.width, 1)];
        lineLab.backgroundColor = [UIColor orangeColor];
        [self addSubview:lineLab];


        UILabel *lineLab1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 242, [UIScreen mainScreen].bounds.size.width, 1)];
        lineLab1.backgroundColor = [UIColor orangeColor];
        [self addSubview:lineLab1];
   
    }
    return self;
}

-(ZFBarChart *)barChart
{
    if (_barChart)
    {
        return _barChart;
    }
    _barChart = [[ZFBarChart alloc] initWithFrame:CGRectMake(-45, 41, O_WIDTH+45, 200)];
    
    self.barChart.dataSource = self;
    self.barChart.delegate = self;
    self.barChart.isResetAxisLineMaxValue = YES;
    [self addSubview:self.barChart];
    [self.barChart strokePath];
    
    
    
    return _barChart;
}


-(void)didTDManagerSecondADTu:(NSNotification *)noti
{
    if (noti.object != 0)
    {
        _adTDic = [noti.object objectForKey:@"data"];
       // NSLog(@" zheg -----====== _adTDic %@",_adTDic);
        
        
        ADHeaderModel *adModel = [ADHeaderModel mj_objectWithKeyValues:noti.object[@"data"]];
        
        //第一层的数据可以直接取值
      //  NSLog(@"这是第一层的数据  =    %@",adModel.all_money);
        _zhuStr = adModel.max_data;
        //开始遍历数据  
        for (ADdataModle *datamoeld in adModel.data)
        {
            [self.dataArray addObject:datamoeld.name];
            [self.twoArray addObject:datamoeld.data];
   
            
        }
        
        //NSLog(@"这里面遍历的是第二层的数据   %@",self.dataArray);

        _allMoneyStr =[NSString stringWithFormat:@"%@",_adTDic[@"all_money"]];
        _adLab.text = [NSString stringWithFormat:@"   广告费用 :%@ 万元",_allMoneyStr];

       [self barChart];
    }
    
}



//保险的写法
-(NSMutableArray *)dataArray{
    
    if (_dataArray) {
        return _dataArray;
    }
    
    _dataArray = [NSMutableArray arrayWithCapacity:0];
    return _dataArray;
}

-(NSMutableArray *)twoArray
{
    if (_twoArray)
    {
        return _twoArray;
    }
    _twoArray = [NSMutableArray arrayWithCapacity:0];
    return _twoArray;
}



-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:TDManagerSecondADTu object:nil];
}
- (NSArray *)valueArrayInGenericChart:(ZFGenericChart *)chart{
    
    
    return _twoArray;
}

- (NSArray *)nameArrayInGenericChart:(ZFGenericChart *)chart{
    return _dataArray;
}

- (NSArray *)colorArrayInGenericChart:(ZFGenericChart *)chart{
    return @[ZFLvColor];
}

- (CGFloat)axisLineMaxValueInGenericChart:(ZFGenericChart *)chart{
    


    return [_zhuStr intValue];
}
- (CGFloat)paddingForGroupsInBarChart:(ZFBarChart *)barChart
{
    return 40;
}
- (NSArray *)gradientColorArrayInBarChart:(ZFBarChart *)barChart{
    ZFGradientAttribute * gradientAttribute1 = [[ZFGradientAttribute alloc] init];
    gradientAttribute1.colors = @[(id)ZFColor(15, 212, 201, 1).CGColor, (id)ZFWhite.CGColor];
    gradientAttribute1.locations = @[@(0.5), @(0.99)];
    gradientAttribute1.startPoint = CGPointMake(0, 0.5);
    gradientAttribute1.endPoint = CGPointMake(1, 0.5);
    
    return [NSArray arrayWithObjects:gradientAttribute1, nil];
}

@end
